import React, { Component } from 'react';
import { connect } from 'dva';
import { Card, Badge, Table, Divider, Button } from 'antd';
import PageHeaderLayout from '../../layouts/PageHeaderLayout';
import DescriptionList from '../../components/DescriptionList';
import styles from './NewsInfo.less';
import queryString from 'query-string';

const { Description } = DescriptionList;

@connect(state => ({
  news: state.news,
}))
export default class NewsInfo extends Component {
  componentDidMount() {
    const { dispatch } = this.props;
    dispatch({
      type: 'news/clear',
    });
    var search = queryString.parse(this.props.location.search);
    if (this.props.location.pathname == "/newsInfo"){
      if (search.id){
        dispatch({
          type: 'news/infoFile',
          payload: {id: search.id}
        });
      }
    } else {
      if (search.id){
        dispatch({
          type: 'news/info',
          payload: {id: search.id}
        });
      }
    }

  }

  render() {
    const { news } = this.props;
    const {title, type, cover, sort, content, appendix} = news;
    var labelArray = ["标题", "类型", "排序", "图片", "内容"];
    var typeName = "";
    switch (type){
      case 1: //律所新闻 type=1
        typeName = "律甲介绍";
        break;
      case 2: //最新成果 type=2
        typeName = "服务产品";
        break;
      case 3: //产品介绍 type=3
        typeName = "合作客户";
        break;
      case 4: //法律资讯 type=4
        typeName = "经典案例";
        break;
      case 5: //客服 type=5
        typeName = "法律咨询";
        break;
      default:
        typeName = '文件上传';
        break;
    }

    var loadFile = [];
    if (this.props.location.pathname == "/fileInfo"){
      loadFile.push(
        <div key="load-newFile-key">
          <Divider style={{ marginBottom: 1 }} />
          <DescriptionList size="large" title="下载" style={{ marginBottom: 32 }}>
            <a href={appendix}><Button>文件上传</Button></a>
          </DescriptionList>
        </div>
      );
    }

    return (
      <PageHeaderLayout title={typeName + "详情"}>
        <Card bordered={false}>
          <DescriptionList size="large" title={title} style={{ marginBottom: 32 }}>
            <Description term="类型">{typeName}</Description>
            <Description term="排序">{sort + ""}</Description>
          </DescriptionList>

          <Divider style={{ marginBottom: 1 }} />
          <DescriptionList size="large" title="图片" style={{ marginBottom: 32 }}>
            <img className={styles.image_div} src={cover} />
          </DescriptionList>

          {loadFile}

          <Divider style={{ marginBottom: 32 }} />
          <DescriptionList size="large" title="内容" style={{ marginBottom: 32 }}>
            <div><span dangerouslySetInnerHTML={{ __html: content }}></span></div>
          </DescriptionList>
          <Divider style={{ marginBottom: 32 }} />
        </Card>
      </PageHeaderLayout>
    );
  }
}
